package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.NewsInfo;
import com.hcrazy.manchester.app.pojo.PageModel;

public class NewsInfoDAO extends BaseDAO<NewsInfo>{

	public long addNewsInfo(NewsInfo newsInfo) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_news_info(");
		sb.append("newsTitle,newsDesc,newsDetails,author,userId,publishTime,newsLogo,newsImages,showInIndex,sortId,views,praiseBaseCount,praiseCount,commentBaseCount,commentCount,newsType");
		sb.append(") values (");
		sb.append(":newsTitle,:newsDesc,:newsDetails,:author,:userId,:publishTime,:newsLogo,:newsImages,:showInIndex,:sortId,:views,:praiseBaseCount,:praiseCount,:commentBaseCount,:commentCount,:newsType");
		sb.append(")");
		
		update(sb.toString(), newsInfo);
		
		return getIncreamentId();
	}
	
	public void updateNewsInfo(NewsInfo newsInfo) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("update him_news_info set ");
		sb.append(" newsTitle=:newsTitle,newsDesc=:newsDesc,newsDetails=:newsDetails,author=:author,userId=:userId,");
		sb.append(" publishTime=:publishTime,newsLogo=:newsLogo,newsImages=:newsImages,showInIndex=:showInIndex,");
		sb.append(" sortId=:sortId,views=:views,praiseBaseCount=:praiseBaseCount,praiseCount=:praiseCount,commentBaseCount=:commentBaseCount,");
		sb.append(" commentCount=:commentCount,newsType=:newsType");
		sb.append(" where newsId = :newsId");
		
		update(sb.toString(), newsInfo);
	}
	
	public List<NewsInfo> listAllInfos(int newsType) throws Exception{
		String sql = "select * from him_news_info where newsType = ?";
		return getList(sql, NewsInfo.class,newsType);
	}
	
	public List<NewsInfo> getAllInfosBySortId(int sortId) throws Exception{
		String sql = "select a.newsTitle,a.newsDesc,a.author,a.userId,a.publishTime," +
				"a.newsLogo,a.newsImages,a.showInIndex,a.sortId,a.views,a.praiseBaseCount," +
				"a.praiseCount,a.commentBaseCount,a.commentCount,a.newsType,b.sortName " +
				"from him_news_info a,him_news_sort b where a.sortId = b.sortId and a.sortId = ?";
		return getList(sql, NewsInfo.class,sortId);
	}
	
	public List<NewsInfo> getAllInfosHasDetailBySortId(int sortId) throws Exception{
		String sql = "select a.*,b.sortName from him_news_info a,him_news_sort b where a.sortId = b.sortId and a.sortId = ?";
		return getList(sql, NewsInfo.class,sortId);
	}
	
	public PageModel getPagedInfos(PageModel model,String search,int sortId,int moduleType) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from (select a.*,b.sortName from him_news_info a,him_news_sort b where a.sortId = b.sortId) as c");
		StringBuilder countSQL = new StringBuilder("select count(*) from (select a.*,b.sortName from him_news_info a,him_news_sort b where a.sortId = b.sortId) as c ");
		StringBuilder whereSQL = new StringBuilder(" where (newsTitle like ?  or author like ?) and newsType = ? ");
		List<Object> paramList = new ArrayList<Object>();
		if(null == search)
			search = "";
		paramList.add("%"+search+"%");
		paramList.add("%"+search+"%");
		paramList.add(moduleType);
		if(sortId > 0){
			whereSQL.append(" and sortId = ?");
			paramList.add(sortId);
		}
		
		whereSQL.append(" order by publishTime desc");
		
//		System.out.println(querySQL+whereSQL.toString());
//		System.out.println(paramList);
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, NewsInfo.class);
	}
	
	public NewsInfo getById(int newsId) throws Exception{
		String sql = "select * from him_news_info where newsId = ?";
		return this.getJavaBean(sql, NewsInfo.class, newsId);
	}
	
	public NewsInfo getById(int newsId,boolean hasDetail) throws Exception{
		if(hasDetail){
			return getById(newsId);
		}
		String sql = "select a.newsTitle,a.newsDesc,a.author,a.userId,a.publishTime," +
				"a.newsLogo,a.newsImages,a.showInIndex,a.sortId,a.views,a.praiseBaseCount," +
				"a.praiseCount,a.commentBaseCount,a.commentCount,a.newsType,b.sortName " +
				"from him_news_info where newsId = ?";
		return this.getJavaBean(sql, NewsInfo.class, newsId);
	}
	
	public void delNewsInfo(int newsId) throws Exception{
		String sql = "delete from him_news_info where newsId = ?";
		this.commonUpdate(sql, newsId);
	}
	
	public long getPraiseCount(long newsId) throws Exception{
		String sql = "select praiseCount from him_news_info where newsId = ?";
		return this.getJdbcTemplate().queryForLong(sql,newsId);
	}
	
	public long getPraiseBaseCount(long newsId) throws Exception{
		String sql = "select praiseBaseCount from him_news_info where newsId = ?";
		return this.getJdbcTemplate().queryForLong(sql,newsId);
	}
	
	public long getCommentCount(long newsId) throws Exception{
		String sql = "select commentCount from him_news_info where newsId = ?";
		return this.getJdbcTemplate().queryForLong(sql,newsId);
	}
	
	public long getCommentBaseCount(long newsId) throws Exception{
		String sql = "select commentBaseCount from him_news_info where newsId = ?";
		return this.getJdbcTemplate().queryForLong(sql,newsId);
	}
	
	public void addPraiseCount(long newsId) throws Exception {
		String sql = "update him_news_info set praiseCount = praiseCount + 1 where newsId = ?";
		this.commonUpdate(sql, newsId);
	}
	
	public void addCommentCount(long newsId) throws Exception {
		String sql = "update him_news_info set commentCount = commentCount + 1 where newsId = ?";
		this.commonUpdate(sql, newsId);
	}
	
	/**
	 * 获取收藏信息
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public List<NewsInfo> getCollegeNews(long userId,int moduleType) throws Exception{
		
		StringBuffer sb = new StringBuffer();
		sb.append("select a.type,b.* from him_user_collection a,him_news_info b");
		sb.append(" where a.userId = ?");
		sb.append(" and a.type = ? and a.infoId = b.newsId ");
		
		return this.getList(sb.toString(), NewsInfo.class,userId,moduleType);
	}
	
	
}
